﻿using System;
using System.Web.UI.WebControls;
using Inkubator.Wizard.Model.Model;
using Inkubator.Wizard.Model.Xml;
using Inkubator.Wizard.Model.Style;
using Inkubator.Wizard.WebWizard.WizardControl;

namespace Inkubator.Wizard.WizardGenerator.Style
{
    public class WizardStyle
    {

        private StyleXml styleXml;
        private StyleWizardView style;

        public WizardStyle( string xmlPath, string xsdPath )
        {
            XmlOperations xml = new XmlOperations();
            WizardModel wizardModel = xml.deserialize(xmlPath, xsdPath);
            styleXml = wizardModel.style;
        }

        public WizardStyle(WizardModel wizardModel)
        {
            this.styleXml = wizardModel.style;
        }

        /// <summary>
        /// This method apply the style obtained from the XML File to the WizardControl object
        /// and apply the default style if the XML File doesn't contained any style.
        /// </summary>
        /// <param name="wizard"></param>
        public void applyStyle( WizardWeb wizard )
        {
            if (styleXml != null)
            {
                style = new StyleWizardView(wizard);
                this.wizardStyle(wizard);
                this.stepStyle(wizard);
                this.sideBarButtonStyle(wizard);
                this.navigationButtonStyle(wizard);
                this.headerStyle(wizard);
            }
            else
            {
                DefaultStyle.applyDefaulStyle(wizard);
            }
        }
        
        private void wizardStyle(WizardWeb wizard)
        {
            //WIZARD
            style.setWizardBackColor(System.Drawing.ColorTranslator.FromHtml( styleXml.wizardStyle.backColor ));
            style.setWizardBorderColor(System.Drawing.ColorTranslator.FromHtml( styleXml.wizardStyle.borderColor ));
        }

        private void stepStyle(WizardWeb wizard)
        {
            //STEP STYLE
            style.setCellCenterForeColor(System.Drawing.ColorTranslator.FromHtml( styleXml.step.foreColor ));
        }

        private void sideBarButtonStyle(WizardWeb wizard)
        {
            //SIDE BAR BUTTON STYLE
            style.setCellLeftBackColor(System.Drawing.ColorTranslator.FromHtml(styleXml.sideBarButton.backColor));
            style.setCellLeftForeColor(System.Drawing.ColorTranslator.FromHtml(styleXml.sideBarButton.foreColor));
        }

        private void navigationButtonStyle(WizardWeb wizard)
        {
            //NAVIGATION BUTTON STYLE
            style.setButtonPanelBackColor(System.Drawing.ColorTranslator.FromHtml( styleXml.navigation.backColor ));
            style.setButtonPanelBorderColor(System.Drawing.ColorTranslator.FromHtml( styleXml.navigation.borderColor ));
            style.setButtonPanelBorderStyle((BorderStyle)Enum.Parse(typeof(BorderStyle),
                styleXml.navigation.borderStyle, true));
            style.setButtonPanelBorderWidth(styleXml.navigation.borderWidth);
            style.setButtonPanelForeColor(System.Drawing.ColorTranslator.FromHtml( styleXml.navigation.foreColor ));
        }

        private void headerStyle(WizardWeb wizard)
        {
            //HEADER STYLE
            style.setTitlePanelBackColor(System.Drawing.ColorTranslator.FromHtml( styleXml.header.backColor ));
            style.setTitlePanelBorderColor(System.Drawing.ColorTranslator.FromHtml( styleXml.header.borderColor ));
            style.setTitlePanelBorderStyle((BorderStyle)Enum.Parse( typeof(BorderStyle),
                styleXml.header.borderStyle, true));
            style.setTitlePanelBorderWidth(styleXml.header.borderWidth);
            style.setTitlePanelFontBold(styleXml.header.fontBold);
            style.setTitlePanelForeColor(System.Drawing.ColorTranslator.FromHtml(styleXml.header.foreColor));
            style.setTitlePanelHorizontalAlign((HorizontalAlign)Enum.Parse(typeof(HorizontalAlign),
                styleXml.header.horizontalAlign, true));
        }

    }
}
